package com.cvmars.jianyou.module.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.model.UserCoustomModel;
import com.cvmars.jianyou.model.UserModel;
import com.cvmars.jianyou.module.adapter.JinengAdapter;
import com.cvmars.jianyou.module.adapter.UserGuanzhuAdapter;
import com.cvmars.jianyou.module.base.BaseActivity;
import com.cvmars.jianyou.ui.pulltorefresh.PulltoRefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class JiNengActivity extends BaseActivity {


    @BindView(R.id.list_friend)
    PulltoRefreshRecyclerView listFriend;

    JinengAdapter homeAdapter;
    List<UserModel> list = new ArrayList<>();
    int mCurPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ji_neng);
        ButterKnife.bind(this);

        homeAdapter = new JinengAdapter(this, R.layout.item_jineng, list);
        listFriend.setAdapter(homeAdapter);
        getLoadDialogAndShow();
        requestData();
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Bundle bundle = new Bundle();
                bundle.putString("userid", list.get(position).id);
                goActivity(bundle, PersonHomeActivity.class);
            }
        });

        listFriend.setPullRefreshListener(new PulltoRefreshRecyclerView.RecyPtrHandler() {
            @Override
            public void onLoadMore() {
                requestData();
            }

            @Override
            public void onRefresh() {

                listFriend.mCurPager = 1;
                requestData();
            }
        });
    }


    public void requestData() {


        HttpUtils.getInstance().toSubscribe(Api.getInstance().getUserList(listFriend.mCurPager)
                , new SimpleSubscriber<HttpResult<UserCoustomModel>>() {
                    @Override
                    protected void _onNext(HttpResult<UserCoustomModel> o) {

                        getLoadDialogAndDismiss();

                        UserCoustomModel data = o.getData();
                        if (data != null) {
                            List<UserModel> results = data.results;
                            if (listFriend.mCurPager == 1) {
                                list.clear();
                            }
                            list.addAll(results);
                            homeAdapter.notifyDataSetChanged();
                            listFriend.refreshComplete();
                            if (data.next == null) {
                                homeAdapter.loadMoreEnd();
                            }
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }
}
